package cn.stylefeng.guns.modular.fi.controller;

import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.basic.entity.BasicOperate;
import cn.stylefeng.guns.modular.basic.service.BasicOperateService;
import cn.stylefeng.guns.modular.fi.entity.FiTempCarAppStand;
import cn.stylefeng.guns.modular.fi.model.params.FiTempCarAppStandParam;
import cn.stylefeng.guns.modular.fi.service.FiTempCarAppStandService;
import cn.stylefeng.guns.sys.modular.system.entity.User;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.guns.sys.modular.system.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;


/**
 * 临时车超额审批标准控制器
 *
 * @author zhangzh
 * @Date 2024-11-19 10:32:33
 */
@Controller
@RequestMapping("/fiTempCarAppStand")
public class FiTempCarAppStandController extends BaseController {

    private String PREFIX = "/fi/fiTempCarAppStand";

    @Autowired
    private FiTempCarAppStandService fiTempCarAppStandService;

    @Autowired
    private UserService userService;

    @Autowired
    private BasicOperateService basicOperateService;

    /**
     * 跳转到主页面
     *
     * @author zhangzh
     * @Date 2024-11-19
     */
    @RequestMapping("")
    public String index(Model model) {
        User user = userService.getcurrentUser();
        Long deptId = user.getDeptId();
        Long deliverId = user.getDeliverId();
        model.addAttribute("basicOperatelist", basicOperateService.findAvailableList(deptId, deliverId, user.getOperateId()));
        return PREFIX + "/fiTempCarAppStand.html";
    }

    /**
     * 新增页面
     *
     * @author zhangzh
     * @Date 2024-11-19
     */
    @RequestMapping("/add")
    public String add(Model model) {
        User user = userService.getcurrentUser();
        Long deptId = user.getDeptId();
        Long deliverId = user.getDeliverId();
        model.addAttribute("basicOperatelist", basicOperateService.findAvailableList(deptId, deliverId, user.getOperateId()));
        return PREFIX + "/fiTempCarAppStand_add.html";
    }

    /**
     * 编辑页面
     *
     * @author zhangzh
     * @Date 2024-11-19
     */
    @RequestMapping("/edit")
    public String edit(Model model) {
        User user = userService.getcurrentUser();
        Long deptId = user.getDeptId();
        Long deliverId = user.getDeliverId();
        model.addAttribute("basicOperatelist", basicOperateService.findAvailableList(deptId, deliverId, user.getOperateId()));
        return PREFIX + "/fiTempCarAppStand_edit.html";
    }

    /**
     * 新增接口
     *
     * @author zhangzh
     * @Date 2024-11-19
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(FiTempCarAppStandParam fiTempCarAppStandParam) {
        User user = userService.getcurrentUser();
        fiTempCarAppStandParam.setCreateUser(user.getName());
        fiTempCarAppStandParam.setCreateTime(new Date());
        BasicOperate basicOperate = basicOperateService.getById(fiTempCarAppStandParam.getOperateId());
        fiTempCarAppStandParam.setOperateName(basicOperate.getOperateName());
        int rangeExist = fiTempCarAppStandService.isRangeExist(FiTempCarAppStandParam.builder()
                .operateId(fiTempCarAppStandParam.getOperateId())
                .kiloStart(fiTempCarAppStandParam.getKiloStart())
                .kiloEnd(fiTempCarAppStandParam.getKiloEnd())
                .build());
        if(rangeExist > 0){
            return ResponseData.error("公里区间有重叠，请检查后重试！");
        }
        this.fiTempCarAppStandService.add(fiTempCarAppStandParam);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     *
     * @author zhangzh
     * @Date 2024-11-19
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(FiTempCarAppStandParam fiTempCarAppStandParam) {
        User user = userService.getcurrentUser();
        fiTempCarAppStandParam.setUpdateUser(user.getName());
        fiTempCarAppStandParam.setUpdateTime(new Date());
        BasicOperate basicOperate = basicOperateService.getById(fiTempCarAppStandParam.getOperateId());
        fiTempCarAppStandParam.setOperateName(basicOperate.getOperateName());

        int rangeExist = fiTempCarAppStandService.isRangeExist(FiTempCarAppStandParam.builder()
                .operateId(fiTempCarAppStandParam.getOperateId())
                .kiloStart(fiTempCarAppStandParam.getKiloStart())
                .kiloEnd(fiTempCarAppStandParam.getKiloEnd())
                .id(fiTempCarAppStandParam.getId())
                .build());
        if(rangeExist > 0){
            return ResponseData.error("公里区间有重叠，请检查后重试！");
        }

        this.fiTempCarAppStandService.update(fiTempCarAppStandParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author zhangzh
     * @Date 2024-11-19
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(FiTempCarAppStandParam fiTempCarAppStandParam) {
        this.fiTempCarAppStandService.delete(fiTempCarAppStandParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author zhangzh
     * @Date 2024-11-19
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(FiTempCarAppStandParam fiTempCarAppStandParam) {
        FiTempCarAppStand detail = this.fiTempCarAppStandService.getById(fiTempCarAppStandParam.getId());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     *
     * @author zhangzh
     * @Date 2024-11-19
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(FiTempCarAppStandParam fiTempCarAppStandParam,String condition ) {
        return this.fiTempCarAppStandService.findPageBySpec(fiTempCarAppStandParam,condition);
    }

}


